﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Common;
using ExpenseSystem.Common.BaseModel;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Fether;
using ExpenseSystem.Model;
using ExpenseSystem.Core.DataAddOn;
using System.Web;

namespace ExpenseSystem.Core.UserModules
{
    public abstract class UserManager:ISystemManager<UserModel>
    {
        protected string tran = HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString();

        protected UserManager() { }
        public static UserManager CreateInstance()
        {
            return new UserBaseManager();
        }
        protected SystemDataUpdater<UserModel> updater = new UserUpdaterRegister().GetUserUpdater();
        public abstract bool Delete(UserModel user,  out string message);
        public abstract bool DeleteById(long[] userId,  out string message);
        public abstract bool Save(UserModel user,  out string message,params IAddable<UserModel>[] addables);
        public virtual bool ChangePwd(long uid,string pwd)
        {
            UserFetcher userFetcher = new UserFetcher();
            UserProfile userProfile = Authority.Authority.GetUserProfile();
            string message;
            if (uid == 0) uid = userProfile.UserId;
            if (!userProfile.Admin && uid != userProfile.UserId)
            {
                message = "非管理员不允许修改其他用户的密码";
                return false;
            }
            UserModel userModel= userFetcher.Get(uid,out message);
            if (userModel == null)
                return false;
            userModel.Password = StringHelper.StringToMD5(pwd);
            Lor.MsSqlMaker<UserModel> msSqlMaker = new Lor.MsSqlMaker<UserModel>();
            userModel.Flag += 1;
            return msSqlMaker.Update(userModel,tran,out message);
        }
    }
}
